const Actions = require('./actions');
const DB = require('app/db.js');
import { Storage } from 'clientConfig/util/StoreData'
import { Toast } from 'saltui';
import base from 'clientConfig/util/jsapi/base';
import { Control } from 'react-keeper';
const htmUrl = require('clientConfig/util/constants').htmUrl;
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        xuankeId: ''
    },

    onFetch: function (params, cb) {
        let t = this;
        if (!params.xuankeId) return;
        t.data.xuankeId = params.xuankeId;
        DB.Parent.get_electivecourses_detail({
            schoolId: Storage.get('user.schoolId'),
            studentId: Storage.get('user.studentId'),
            xuankeId: params.xuankeId
        })
            .then((content) => {
                if (content.code == 1) {
                    if (content && content.data) {
                        if (content.data.courseList instanceof Array && content.data.courseList.length > 0) {
                            for (let item of content.data.courseList) {
                                item.selected = false;
                            }
                            t.data.courseList = content.data.courseList;
                        }
                        t.data.itemsList = content.data.itemsList;
                        cb && cb(content.data.itemsList);
                        t.updateComponent();
                    }
                } else {
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {

                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onSubmit: function (params, cb) {
        let t = this;
        if (!params.itemsId) return;
        DB.Parent.save_elective_courses({
            schoolId: Storage.get('user.schoolId'),
            studentId: Storage.get('user.studentId'),
            xuankeId: t.data.xuankeId,
            itemsId: params.itemsId
        })
            .then((content) => {
                if (content.code == 1) {
                    Toast.show({
                        type: 'success',
                        content: '提交成功'
                    });
                    Control.go(-1)
                } else {
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onGethelp: function (params, cb) {
        let t = this;
        DB.Parent.get_elective_help({
        })
            .then((content) => {
                if (content.code == 1) {
                    if (content.data && content.data.linkUrl) {
                        let url = htmUrl + content.data.linkUrl;
                        base.openLink(url);
                    }
                } else {
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
